package com.ljf.eslab.lab.controller;

import java.util.Arrays;
import java.util.Map;

import com.ljf.eslab.lab.annotation.RedisCacheEvict;
import com.ljf.eslab.lab.annotation.RedisCachePut;
import com.ljf.eslab.lab.annotation.RedisCacheable;
import com.ljf.eslab.lab.service.TeacherService;
import com.ljf.eslab.lab.service.UserTokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import com.ljf.eslab.lab.entity.CourseEntity;
import com.ljf.eslab.lab.service.CourseService;
import com.ljf.common.utils.PageUtils;
import com.ljf.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 课程信息表
 *
 * @author ljf
 * @email ljf@gmail.com
 * @date 2021-05-28 09:12:59
 */
@Slf4j
@RestController
@RequestMapping("lab/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private UserTokenService userTokenService;
    @Autowired
    private TeacherService teacherService;

    /**
     * 列表
     */
    @Cacheable(value = "courses")
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = courseService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RedisCacheable(value = "course",key = "#tid")
    @RequestMapping("/listByTID")
    public R listByTID( @RequestAttribute("tid") Long tid,@RequestParam Map<String, Object> params) {
        PageUtils page;
        if (tid == null) {
            page = courseService.queryPage(params);
        } else {
            page = courseService.queryPageByTid(params, tid);
        }

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RedisCacheable(value = "course",key = "#id")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        CourseEntity course = courseService.getById(id);

        return R.ok().put("course", course);
    }

    /**
     * 保存
     */
    @RedisCacheable(value = "course",key = "#")
    @RequestMapping("/save")
    public R save(HttpServletRequest request,@RequestBody CourseEntity course) {
        Integer tid = (Integer) request.getAttribute("tid");
        log.debug("teacherID:{}",tid);
        if (tid == null)
            return R.error("不是老师，无权限选课");
        course.setTeacherId(tid);
        courseService.save(course);

        return R.ok();
    }
    /**
     * 修改
     */
    @RedisCachePut(value = "course", key = "#course.id")
    @RequestMapping("/update")
    public R update(@RequestBody CourseEntity course) {
        courseService.updateById(course);

        return R.ok();
    }

    /**
     * 删除
     */
    @RedisCacheEvict(value = "courses")
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        courseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
